package com.ms.rc_lab;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import androidx.appcompat.widget.AppCompatButton;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Vector;

public class HorizontalAdapter extends ArrayAdapter<String> {
    private Context context;
    private final String TAG = "HorizontalAdapter";
    public enum Mode{
        MODE_SERVO,
        MODE_DC
    }
    private Mode mMode;
    public class CheckItem{
        public int item;
        public boolean checked;
        public boolean reverse;
        public VerticalSeekBar seekBar;
    }

    private ArrayList<CheckItem> checkedItems = new ArrayList<>();


    private CompoundButton.OnCheckedChangeListener checkedChangeListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            Log.d(TAG, String.format("%s %b", buttonView.getTag(), isChecked));
            int item = Integer.valueOf(buttonView.getTag().toString());
            checkedItems.get(item).item = item;
            checkedItems.get(item).checked = isChecked;
        }
    };

    private SeekBar.OnSeekBarChangeListener seekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            Log.d(TAG, String.format("%s %d", seekBar.getTag(), progress));
            int channel = Integer.valueOf(seekBar.getTag().toString());
            String cmds = "";
            for (CheckItem item: checkedItems){
                if(item.checked){
                    int curProgress = 0;
                    if(!item.reverse){
                        curProgress = progress;
                    }else {
                        curProgress = 100 - progress;
                    }
                    item.seekBar.setProgressAndThumbNoRecall(curProgress);
                    if(mMode == Mode.MODE_DC){
                        curProgress = (int)((curProgress - 50)/50.0f * 100);
                        String cmd = CommandCenter.commandCat(CommandCenter.MOT, String.valueOf(item.item), curProgress);
                        cmds += cmd;
                    }else{
                        curProgress = (int)((curProgress - 50)/50.0f * 90);
                        String cmd = CommandCenter.commandCat(CommandCenter.PWM, String.valueOf(item.item), curProgress);
                        cmds += cmd;
                    }
                }else if(item.item == channel){
                    if(mMode == Mode.MODE_DC){
                        int curProgress = (int)((progress - 50)/50.0f * 100);
                        String cmd = CommandCenter.commandCat(CommandCenter.MOT, String.valueOf(item.item), curProgress);
                        cmds += cmd;
                    }else{
                        int curProgress = (int)((progress - 50)/50.0f * 90);
                        String cmd = CommandCenter.commandCat(CommandCenter.PWM, String.valueOf(item.item), curProgress);
                        cmds += cmd;
                    }
                }
            }
            TcpClient.getInstance().sendMessage(cmds);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    };

    private CompoundButton.OnCheckedChangeListener switchListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            int item = Integer.valueOf(buttonView.getTag().toString());
            checkedItems.get(item).item = item;
            checkedItems.get(item).reverse = isChecked;
        }
    };

    View.OnClickListener btnListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int item = Integer.valueOf(v.getTag().toString());
            checkedItems.get(item).seekBar.setProgressAndThumbNoRecall(50);
            if(mMode == Mode.MODE_DC){
                int curProgress = 0;
                String cmd = CommandCenter.commandCat(CommandCenter.MOT, String.valueOf(item), curProgress);
                TcpClient.getInstance().sendMessage(cmd);
            }else{
                int curProgress = 0;
                String cmd = CommandCenter.commandCat(CommandCenter.PWM, String.valueOf(item), curProgress);
                TcpClient.getInstance().sendMessage(cmd);
            }
        }
    };


    public HorizontalAdapter(Context context, int resource, List<String> items, Mode mode) {
        super(context, resource, items);
        this.context = context;
        this.mMode = mode;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.motor_item, parent, false);
        TextView textView = rowView.findViewById(R.id.text);
        textView.setText(getItem(position));
        CheckBox checkBox = rowView.findViewById(R.id.checkbox);
        checkBox.setOnCheckedChangeListener(checkedChangeListener);
        checkBox.setTag(String.format("%d", position));
        VerticalSeekBar seekBar = rowView.findViewById(R.id.seekBar);
        seekBar.setTag(String.format("%d", position));
        seekBar.setOnSeekBarChangeListener(seekBarChangeListener);
        Switch sw = rowView.findViewById(R.id.switch_des);
        sw.setTag(String.format("%d", position));
        sw.setOnCheckedChangeListener(switchListener);
        AppCompatButton button = rowView.findViewById(R.id.buttonReset);
        button.setTag(String.format("%d", position));
        button.setOnClickListener(btnListener);
        CheckItem item = new CheckItem();
        item.item= position;
        item.checked = checkBox.isChecked();
        item.seekBar = seekBar;
        item.reverse = sw.isChecked();
        checkedItems.add(item);
        return rowView;
    }

}
